import validHtml from 'ngtemplate!html!../../../tpls/dialog/valid.dialog.html';

class GroupEditController {
    constructor($rootScope, $scope, $cookieStore, $timeout, $state, ngDialog, dialogService, GroupService, getCourseList, getGroupaliasList, getGroupDetail){

        const vm = this;

        vm.$rootScope        = $rootScope;
        vm.$cookieStore      = $cookieStore;
        vm.$timeout          = $timeout;
        vm.$state            = $state;
        vm.ngDialog          = ngDialog;
        vm.dialogService     = dialogService;
        vm.GroupService      = GroupService;
        vm.getCourseList     = getCourseList;
        vm.getGroupaliasList = getGroupaliasList;
        vm.getGroupDetail    = getGroupDetail;

        // 科目列表
        vm.getCourseList && vm.getCourseList.code ==0 && (vm.courselist = vm.getCourseList.data);

        // 分组列表
        vm.getGroupaliasList && vm.getGroupaliasList.code ==0 && (vm.groupaliaslist = vm.getGroupaliasList.data);

        // 表单数据初始化
        vm.group = {
            group_title: '',
            group_intro: '',
            course: '',
            groupalias: ''
        }

        console.log(vm.getGroupDetail)

        // 获取小组详情
        if (vm.getGroupDetail && vm.getGroupDetail.code == 0) {
            const Detail = vm.getGroupDetail.data;
            
            vm.group.group_title = Detail.group_title;
            vm.group.group_intro = Detail.group_intro;

            // 渲染页面显示所属科目
            angular.forEach(vm.courselist, (value, index) => Detail.course_id == value.course_id && (vm.group.course = value))

            // 渲染页面显示所属分组
            angular.forEach(vm.groupaliaslist, (value, index) => Detail.group_name == value.group_name && (vm.group.groupalias = value))
        }

    }


    // 表单提交
    submitForm(isValid) {

        const vm = this;

        // 验证失败, 提示用户
        if (!isValid) {
            if (!vm.group.course) {
                $.blink(angular.element(document.getElementsByName('course')), 5, 100)
            } else if (!vm.group.groupalias) {
                $.blink(angular.element(document.getElementsByName('groupalias')), 5, 100)
            } else if (!vm.group.group_title) {
                $.blink(angular.element(document.getElementsByName('groupTitle')), 5, 100)
            }
            return false;
        }

        // 验证通过
        if (isValid) {

            // 拼接POST数据格式
            const params = {
                group_title: vm.group.group_title,
                group_intro: vm.group.group_intro,
                course_id: vm.group.course.course_id,
                group_name: vm.group.groupalias.group_name
            }

            console.log(params)

            // 更新小组
            vm.GroupService.postGroupUpdate(params, vm.$cookieStore.get('access_token'), vm.$state.params.group_id)
            .then( data => {

                // 成功调用
                data.code==0&&vm.dialogService.success('更新成功', 'dashboard.race.grouplist');

                // 调用失败, 提示用户
                (data.code!=0&&data.code!=-1001)&&vm.dialogService.error(data.message);

                // 登录信息已过期, 请重新登录
                data.code==-1001&&vm.dialogService.error(data.message, true);
                
                console.log(data)
            }, error => console.log('error'));
        }

        return false;
    }

    // 删除计时点
    deleteItems(group_id) {
        
        const vm = this;

        // 模态框显示文本
        vm.$rootScope.common = {
            text : '是否删除小组信息'
        }

        vm.ngDialog.open({
            template: validHtml,
            className: 'ngdialog-theme-default ngdialog-alert',
            scope: vm.$rootScope
        });

        // 提交表单
        vm.$rootScope.submitDelForm = (isValid) => {

            // 验证失败, 提示用户
            if (!isValid) {
                vm.dialogService.error();
            }

            // 验证通过
            if (isValid) {

                // 关闭模态框
                vm.ngDialog.close();

                vm.GroupService.postGroupDel({group_id: group_id}, vm.$cookieStore.get('access_token'), vm.$state.params.race_id)
                .then( data => {
                    
                    // 成功调用
                    data.code==0&&vm.dialogService.success('小组删除成功', 'dashboard.race.grouplist');
                    
                    // 调用失败, 提示用户
                    (data.code!=0&&data.code!=-1001)&&vm.dialogService.error(data.message);

                    // 登录信息已过期, 请重新登录
                    data.code==-1001&&vm.dialogService.error(data.message, true);

                    console.log(data)
                }, error => console.log('error'));
            }

            return false;
        }  
    }
}

GroupEditController.$inject = ['$rootScope', '$scope', '$cookieStore', '$timeout', '$state', 'ngDialog', 'dialogService', 'GroupService', 'getCourseList', 'getGroupaliasList', 'getGroupDetail']; 

export default GroupEditController;